﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Text;
using Common;

public partial class website_admin_knowlegebase_fileeditor : PageBase
{ 
    string TitleImage = ConfigurationManager.AppSettings["TitleImage"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("articleid") != "")
                Initialize();
            else
            {
                txtPublishDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                txtTitle.Value = DateTime.Now.ToString("yyyyMMdd");
            }
        }
    }
 
    #region 数据初始化
    void Initialize()
    {
        Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(RequestBase.GetQueryString("articleid"));
        ArticleObj = _obj; 
    }

    Model.KbArticleBase ArticleObj
    {
        get
        {
            Model.KbArticleBase _article = new Model.KbArticleBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("articleid")))
                _article.ArticleId = RequestBase.GetQueryString("articleid");

            _article.Title = txtTitle.Value.Trim(); 

            _article.CategoryId = BLL.KbCategoryBase.GetCategoryIdByNumber(RequestBase.GetQueryString("c_number"));

            _article.PublishDate = DateTime.Parse(txtPublishDate.Value.Trim());
            _article.ModifiedOn = DateTime.Now;
            _article.CreatedOn = DateTime.Now;
            _article.ModifiedBy = this.SystemUserId;
            _article.CreatedBy = this.SystemUserId; 
            _article.TitlePic = txtUploadControl_TitleImage.Value.Trim().Replace("|", "");

            return _article;
        }
        set
        {
            if (value != null)
            {
                txtTitle.Value = value.Title.Trim();

                txtPublishDate.Value = value.PublishDate.ToString("yyyy-MM-dd");
                if (value.TitlePic != "")
                {
                    txtUploadControl_TitleImage.Value = "|" + value.TitlePic.Trim();
                    divTitleImageList_TitleImage.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.TitlePic.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + TitleImage + value.TitlePic.Trim().Split('*')[1] + "' title='单击打开文件' target='_blank'>" + value.TitlePic.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.TitlePic.Trim() + "' itemid='TitleImage' onclick='uploadinit($(this));'>[删除]</a></div>";
                } 
            }
        }
    }
 
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.KbArticleBase.Add(ArticleObj, null);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.KbArticleBase.Update(ArticleObj, null);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.LoadingData(); window.close();");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
